package GUI;

import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
//import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.*;
import com.sun.lwuit.List;
//import java.util.Hashtable;
//import java.io.*;
import java.util.*;

/**
 * @author Rudi
 */
public class GUI extends MIDlet implements ActionListener  {
    //forms
    Form home;
    //commands
    Command exitCommand;
    
    //buttons
    Button up;      //up, down, left and right buttons
    Button down;
    Button left;
    Button right;

    //images
    Image image;    //used to store banner image
    //labels
    Label banner;   //used to display banner
    //lists
    //List list;      //used as a list for user items
    //text areas
    TextArea big;   //used to store the current plot text
    TextArea small;   //used to store the current plot text

    //Gameplay viariables:************************************
    private GameWorld game;
    private Location locationOld  = null;
    private Location locationNew  = null;
    private ItemBag playerBag = new ItemBag();
    private String command = null;
    private boolean validCommand = false;
    private String needItem = null;
    private String giveItem = null;
    //*************************************************
 
    public void startApp() {
        Display.init(this); //initialise display
        setTheme();
        game = (new CreateGameWorld()).getGameWorld();
        locationNew  = game.getCurrentLocation();
        createUI();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    //***********************************************************
    // All the game's logic was implimented in the action listener
    //***********************************************************
    public void actionPerformed(ActionEvent ae) 
    {
        //********************************
        //PURE GAME LOGIC IMPLIMENTATION
        //Get the items from the location

        //The following if executes when ANY button is pressed and there is a blockage in the way
        if (!( playerBag.containsItem(needItem) || needItem == null ))
        {
            //return the Location to the previous state
            game.setCurrentLocation(locationOld);
            big.setText( game.getLocationString());
            needItem = null; //make sure the if is not excecuted again

            //return the buttons to their normal states
            grayOutButtons ( game.getExitsString());
            setButtonText ("NORTH","SOUTH","WEST","EAST");

            //The rest of the method is not accessed
            return;
        }


        locationOld //= locationNew;
        /*locationNew*/ = game.getCurrentLocation();


        //Test what button was pressed
        if (ae.getCommand() == exitCommand)
        {
            notifyDestroyed();
        }
        else if(ae.getSource() == up)
        {
            command = "N";
        }
        else if(ae.getSource() == down)
        {
            command = "S";
        }
        else if(ae.getSource() == left)
        {
            command = "W";
        }
        else if(ae.getSource() == right)
        {
            command = "E";
        }

       // Search for an exit match
       for (Enumeration e = game.getCurrentLocation().getExits().elements(); e.hasMoreElements();)
       {
                Exit an_exit = (Exit) e.nextElement();

                if (    (an_exit.getDirectionName().compareTo(command) == 0) ||
                        (an_exit.getShortDirectionName().compareTo(command) == 0 )   )
                {
                        // Set location to the location pointed to by exit
                        game.setCurrentLocation( an_exit.getLeadsTo() );
                        // Valid command encountered
                        validCommand = true;
                        // No need to search exits anymore
                        break;
                }
       }

       //Disable buttons with no directions attached to them
       grayOutButtons ( game.getExitsString());

       //Find the avialiable items from the location
       needItem = game.getCurrentLocation().getNeedItem();
       giveItem = game.getCurrentLocation().getItem();

       //Test for blockage
       if ( playerBag.containsItem(needItem) || needItem == null )
       {
            big.setText( game.getLocationString());

        }
        else //if there is an blockage in the way
        {
            big.setText(game.getBlockageString());
            grayOutButtons("N E S W");
            setButtonText ("BACK","BACK","BACK","BACK");
        }

        //Put in bag
        if(!(playerBag.containsItem(giveItem) || giveItem == null))
        {
            playerBag.addItem(giveItem);
            small.setText(small.getText() + "\n " + giveItem);
        }



    }
    //*****************************************
    //Set the button's text
    //Srting args: up, down, left, right
    //*****************************************
    private void setButtonText (  String sUp, String sDown, String sLeft, String sRight)
    {
       up.setText(sUp);
       down.setText(sDown);
       left.setText(sLeft);
       right.setText(sRight);

    }

    //********************************************
    //Grays out buttons that musn't be clicked
    //arguments: "N S E W" for buttons not to be grayed out
    //********************************************
    private void grayOutButtons (String arg)
    {
       up.setEnabled(false);
       down.setEnabled(false);
       left.setEnabled(false);
       right.setEnabled(false);
       if(arg.indexOf( "N" ) > -1)
       {
           up.setEnabled(true);
       }
       if(arg.indexOf( "S" ) > -1)
       {
           down.setEnabled(true);
       }
       if(arg.indexOf( "E" ) > -1)
       {
           right.setEnabled(true);
       }
       if(arg.indexOf( "W" ) > -1)
       {
           left.setEnabled(true);
       }
    }

    private void createUI()  {  //function to create the GUI and display
        home = new Form("SkiFree Adventure Game"); //create new form
        //home.setLayout(new BorderLayout());
        //create exit button
        exitCommand = new Command("Exit");
        home.addCommand(exitCommand);
        home.setCommandListener(this);

        //text area
        big = new TextArea( game.getLocationString(),3,40,TextArea.UNEDITABLE);
        big.setFocusPainted(false);
        home.addComponent(big);

        //Add direction buttons
        up = new Button("NORTH");
        home.addComponent(up);
        up.addActionListener(this);

        down = new Button("SOUTH");
        home.addComponent(down);
        down.addActionListener(this);

        left = new Button("WEST");
        home.addComponent(left);
        left.addActionListener(this);
        
        right = new Button("EAST");
        home.addComponent(right);
        right.addActionListener(this);

        //Gray out the applicable buttons
        grayOutButtons ( game.getExitsString());

        //item list
        //text area
        small = new TextArea( "Items: ",3,40,TextArea.UNEDITABLE);
        small.setFocusPainted(false);
        home.addComponent(small);

        /*//example for deleting item:
        com.sun.lwuit.list.ListModel mdl = list.getModel();
        mdl.removeItem(2);
        list.setModel(mdl);*/

        home.show();
    }

    //Automated Method
    public void setTheme()
    {
        UIManager uim = UIManager.getInstance();
        Hashtable ht = new Hashtable();
        ht.put("sel#" + Style.BG_COLOR, "660099");
        ht.put(Style.BG_COLOR, "333366");
        ht.put(Style.FG_COLOR, "cccccc");
        uim.setThemeProps(ht);
    }
    

}
